<template>
  <div class="daily-update-rank">
    <div class="sub-title">设备异常报警类型排名</div>
    <div class="common-row">
      <div class="rank text-overflow">排名</div>
      <div class="alarm-type text-overflow">报警类型</div>
      <div class="alarm-bar transparent"></div>
      <div class="alarm-num text-overflow">报警次数</div>
    </div>
    <div class="common-row type-rank-row" v-for="(item, index) in alarmTypeArr" :key="index + '1'">
      <img class="rank text-overflow rank-img" :src="getRankImgUrl(index)">
      <div class="alarm-type text-overflow">{{item.name}}</div>
      <div class="alarm-bar">
        <div class="bar-num" :style="{width: `${item.percent}%`}"></div>
      </div>
      <div class="alarm-num text-overflow">{{item.amount}}</div>
    </div>

    <div class="sub-title">高频异常设备排名</div>
    <div class="common-row">
      <div class="rank text-overflow">排名</div>
      <div class="device-id text-overflow">设备编号</div>
      <div class="position-id text-overflow">位置编号</div>
      <div class="alarm-num text-overflow">报警次数</div>
    </div>
    <div class="common-row device-rank-row" v-for="(item, index) in alarmDeviceArr" :key="index">
      <img class="rank text-overflow rank-img" :src="getRankImgUrl(index)">
      <div class="device-id text-overflow">{{item.deviceId}}</div>
      <div class="position-id text-overflow">{{item.positionId}}</div>
      <div class="alarm-num text-overflow">{{item.alarmNum}}</div>
    </div>
  </div>
</template>

<script>
  import Config from '../../../config'
  export default {
    name: 'DailyUpdateRank',
    data () {
      return {
        alarmTypeArr: [
          {
            name: '',
            amount: 0,
            percent: 0
          }, {
            name: '',
            amount: 0,
            percent: 0
          }, {
            name: '',
            amount: 0,
            percent: 0
          }
        ],
        alarmDeviceArr: [],
        exceptionMap: new Map()
      }
    },
    props: {
      schedule: {
        type: Number,
        default: 0
      }
    },
    watch: {
      schedule (v1, v2) {
        console.log('该更新了')
        this.refresh()
      }
    },
    created () {
      this.initExceptionMap()
    },
    methods:  {
      initExceptionMap () {
        this.exceptionMap.set('wp_exception', '水压异常')
        this.exceptionMap.set('sensor_exception', '传感器异常')
        this.exceptionMap.set('battery_exception', '低电异常')
      },
      getRankImgUrl (index) {
        return `/static/image/monitor-center/new/${index + 1}.png`
      },
      refresh () {
        this.getAlarmTypeNumRank()
        this.getAlarmDeviceNumRank()
      },
      getAlarmTypeNumRank () {
        const url = `${Config.FIREPLUG}/fireplug/alarm/count4`
        this.$http.get(url)
          .then(data => {
            this.getSortedArray(data)
          })
          .catch(e => {})
      },
      getSortedArray (obj) {
        const valueArr  = []
        const total = obj.total
        for (let key in obj) {
          if (obj.hasOwnProperty(key) && key !== 'total') {
            valueArr.push(obj[key])
          }
        }
        valueArr.sort((a,b) => b - a)
        valueArr.forEach((item, index) => {
          this.alarmTypeArr[index].name = this.exceptionMap.get(this.getKeyFromObject(obj, item))
          this.alarmTypeArr[index].amount = item
          this.alarmTypeArr[index].percent = this.getPercent(item, total)
        })
        console.log(this.alarmTypeArr)
      },
      getPercent (val, total) {
        if (!val) {
          return 0
        }
        return ~~(val / total * 100)
      },
      getKeyFromObject (target, value) {
        for (let key in target) {
          if (target.hasOwnProperty(key) && value === target[key]) {
            return key
          }
        }
      },
      getAlarmDeviceNumRank () {
        const url = `${Config.FIREPLUG}/fireplug/alarm/count5`
        this.$http.get(url)
          .then(data => {
            this.alarmDeviceArr = []
            data.some((item, index) => {
              if (index === 3) {
                return true
              }
              this.alarmDeviceArr.push({
                deviceId: item[0],
                positionId: item[1],
                alarmNum: item[2],
              })
            })
            console.log(this.alarmDeviceArr)
          })
          .catch(e => {})
      }
    }
  }
</script>

<style lang="scss" scoped>
  .daily-update-rank {
    .sub-title {
      color: #598CC9;
      margin-bottom: 17px;
      margin-top: 21px;
      font-size: 14px;
      font-weight: bold;
    }
    .sub-title:first-child {
      margin-top: 0;
    }
    .common-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      color: #E28C66;
      font-size: 12px;
      box-sizing: border-box;
      padding: 0 10px;
      margin-bottom: 12px;
      .text-overflow {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      .rank {
        width: 25px;
        margin-right: 22px;
      }
      .rank-img {
        width: 15px;
        margin-right: 35px;
      }
      .device-id {
        width: 135px;
      }
      .alarm-num {
        width: 49px;
      }
      .alarm-bar {
        flex: 1;
        margin-right: 20px;
        height: 8px;
        background-color: #193F80;
        border-radius: 4px;
        position: relative;
        .bar-num {
          background: linear-gradient(90deg, #0A85E9, #1EF7FF);
          border-radius: 4px;
          height: 100%;
          transition: width 1.2s;
        }
      }
      .transparent {
        background-color: transparent
      }
      .alarm-type {
        width: 65px;
        margin-right: 20px;
      }
      .position-id {
        width: 110px;
      }
    }
    .type-rank-row {
      color: #12CEFF;
    }
    .device-rank-row {
      color: #12CEFF;
      margin-bottom: 15px;
    }
    .device-rank-row:first-child {
      margin-bottom: 0;
    }
  }
</style>
